package com.icss.ah.sorting.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.sortting.service.SorttingService;
import com.icss.j2ee.util.UUID;
/**
 * 
 * @author wj
 *
 */
@Controller
@Scope("prototype")
public class SorttingAction extends BaseAction{
	@Autowired
	private SorttingService sorttingService;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	// json串
	private Json jsonText;
	/**
	 * 进入产品分类界面
	 * @return
	 */
	public String toSortList(){
		try {
			request = this.getRequest();
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("Sortting|异常：",e);
		}
		return "sortList";
	}
	
	/**
	 * 
	 * 查询分类
	 */
	public void querySortList(){
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId=userInfo.getCorpId();   //公众号id 
		
		String code=request.getParameter("code");
		code = code==null ? "" : code;
		
		String levell=request.getParameter("level");
		levell = levell==null ? "" : levell;
		int level=1;
		try {
			if(levell==""){
				level=1;
			}else{
				 level = Integer.parseInt(levell)+1;
			}
		} catch (NumberFormatException e) {
		    e.printStackTrace();
		}
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("level",level); //层级
			map.put("code",code);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> sortList = new ArrayList();
			sortList = sorttingService.getSortList(map);
			long sortCount = sorttingService.getSortCount(map);//查询总记录数
			dataGridJson.setRows(sortList);
			dataGridJson.setTotal(sortCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("Sortting|公众号id"+corpId+"商铺id"+shopId+"查询一级分类成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("Sortting|查询一级分类失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 添加或修改分类
	 * @return
	 */
	public String saveSort() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String seriesName = request.getParameter("seriesName"); //分类名称
			String act = request.getParameter("act"); //判断是添加还是修改分类的标识符（自定义的）
			//String userCode = getRequest().getSession().getAttribute("userCode") == null ? "" : getRequest().getSession().getAttribute("userCode").toString();
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();		//商铺id
			String corpId=userInfo.getCorpId();   //公众号id 
			String tUuid=request.getParameter("doId"); //分类id
			tUuid = tUuid ==null ? "":tUuid;
			String pCode=request.getParameter("pCode"); //父级分类编码
			pCode = pCode == null ? "" : pCode;
			
			HashMap map = new HashMap();
			map.put("tUuid",tUuid);
			map.put("shopId",shopId);
			map.put("corpId",corpId);
			map.put("pCode",pCode);
			map.put("act",act);
			map.put("seriesName",seriesName);
			
			boolean result = sorttingService.saveSort(map);
			
			
			jsonText.setSuccess(result);
			if(result){
				jsonText.setMsg("保存成功！");
			} else {
				jsonText.setMsg("保存失败！");
			}
			infolog.info("Sortting|saveSort|result|"+result);
		}catch (Exception e) {
			jsonText.setMsg("保存失败！");
			jsonText.setSuccess(false);
			errlog.error("Sortting|保存一级分类失败：",e);
		}
		return "jsonText";
	}
	
	
	/**
	 * 删除分类
	 * 
	 */
	public String delSort(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String uuid=request.getParameter("uuid"); //分类id，唯一主键
			HashMap map = new HashMap();
			map.put("uuid", uuid);
			sorttingService.delSort(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("删除成功！");
			infolog.info("Sortting|删除分类成功");
		} catch (Exception e) {
			jsonText.setMsg("删除失败！");
			jsonText.setSuccess(false);
			errlog.error("Sortting|删除分类失败：",e);
		}
		return "jsonText";
		
	}
	
	
	
	/**
	 * 更新图片的pic
	 * @return
	 */
	public String updatePic(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String sortPic=request.getParameter("sortPic"); //图片pic
			String addPicId=request.getParameter("addPicId"); //帐号id，唯一主键
			
			HashMap map = new HashMap();
			map.put("sortPic", sortPic);
			map.put("addPicId", addPicId);
			sorttingService.updatePic(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("保存成功！");
			infolog.info("Sortting|更新图片成功");
		} catch (Exception e) {
			jsonText.setMsg("保存失败！");
			jsonText.setSuccess(false);
			errlog.error("Sortting|更新图片失败：",e);
		}
		return "jsonText";
		
	}
	
	/**
	 * 显示图片
	 */
	public void showPic() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String pic=request.getParameter("pic");
			String imageUrl = ResourceLoader.getInstance("icss").getString("imageUrl");
			String theUrl=imageUrl+pic;
			jsonText.setSuccess(true);
			jsonText.setObj(theUrl);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
		
	}
	/**
	 * 修改排序
	 * @return
	 */
	public String changeOrder(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String uuid=request.getParameter("uuid"); //分类id
			String sortNumo=request.getParameter("sortNum"); //该分类修改后的顺序
			String beNumo=request.getParameter("beNum"); //该分类原来的顺序
			String levelo=request.getParameter("level"); //分类级别
			int level=Integer.parseInt(levelo);
			int sortNum=0;
			try {
				sortNum = Integer.parseInt(sortNumo);
			} catch (NumberFormatException e) {
			    e.printStackTrace();
			}
			
			int beNum=0;
			try {
				if(beNumo.equals("")||beNumo==null){
					beNum=0;
				}else{
					beNum = Integer.parseInt(beNumo);
				}
			
			} catch (NumberFormatException e) {
			    e.printStackTrace();
			}
			HashMap map = new HashMap();
			map.put("uuid", uuid);
			map.put("sortNum", sortNum);
			map.put("beNum", beNum);
			map.put("level",level);
			
			boolean result=sorttingService.changeOrder(map); 
			if(result==true){
				jsonText.setSuccess(true);
				jsonText.setMsg("保存成功！");
			}else{
				jsonText.setMsg("保存失败！");
				jsonText.setSuccess(false);
			}
			
			infolog.info("Sortting|修改排序成功");
		} catch (Exception e) {
			jsonText.setMsg("保存失败！");
			jsonText.setSuccess(false);
			errlog.error("Sortting|修改排序失败：",e);
		}
		return "jsonText";
		
	}
	
	/**
	 * 查询总分类数
	 */
	public void querySortAll() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();		//商铺id
			String corpId=userInfo.getCorpId();   //公众号id 

			HashMap map = new HashMap();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			
			Long sortAll=sorttingService.getSortAll(map);
			jsonText.setSuccess(true);
			jsonText.setObj(sortAll);
			infolog.info("Sortting|查询总分类数成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
			errlog.error("Sortting|查询总分类数失败：",e);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
		
	}
	public Json getJsonText() {
		return jsonText;
	}
	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	
}
